package com.fitz.core.filter;

import com.fitz.core.context.RequestContext;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.*;

/**
 * @author gousl01
 * @date 2021/3/15 15:58
 * @desc
 */
public abstract class AbstractFilter implements IFitzFilter,Comparable<AbstractFilter>{

    /**
     * 路由类型 pre route post
     * @return
     */
    public abstract String routeType();

    /**
     * 同种类型的顺序
     * @return
     */
    public abstract int order();

    public Object runFilter(){
        RequestContext rtx = RequestContext.getCurrentContext();
        if (shouldFilter()){
            Object res = run();
            return res;
        }
        return null;
    }

    public Map<String, List<String>> getQueryParams() {
        Map<String, List<String>> qp = RequestContext.getCurrentContext().getRequestQueryParams();
        if (qp != null) return qp;
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        qp = new LinkedHashMap<String, List<String>>();
        if (request.getQueryString() == null){
            return null;
        };
        StringTokenizer st = new StringTokenizer(request.getQueryString(), "&");
        int i;
        while (st.hasMoreTokens()) {
            String s = st.nextToken();
            i = s.indexOf("=");
            if (i > 0 && s.length() >= i + 1) {
                String name = s.substring(0, i);
                String value = s.substring(i + 1);
                try {
                    name = URLDecoder.decode(name, "UTF-8");
                } catch (Exception e) {
                }
                try {
                    value = URLDecoder.decode(value, "UTF-8");
                } catch (Exception e) {
                }

                List<String> valueList = qp.get(name);
                if (valueList == null) {
                    valueList = new LinkedList<String>();
                    qp.put(name, valueList);
                }

                valueList.add(value);
            } else if (i == -1) {
                String name=s;
                String value="";
                try {
                    name = URLDecoder.decode(name, "UTF-8");
                } catch (Exception e) {
                }

                List<String> valueList = qp.get(name);
                if (valueList == null) {
                    valueList = new LinkedList<String>();
                    qp.put(name, valueList);
                }

                valueList.add(value);

            }
        }
        RequestContext.getCurrentContext().setRequestQueryParams(qp);
        return qp;
    }

    public HashMap<String, String> buildRequestQueryParams(
            HttpServletRequest request) {
        Map<String, List<String>> map = this.getQueryParams();
        HashMap<String, String> params = new HashMap<>();
        if (map == null) {
            return params;
        }
        for (String key : map.keySet()) {
            for (String value : map.get(key)) {
                params.put(key, value);
            }
        }
        return params;
    }

    @Override
    public int compareTo(AbstractFilter filter) {
        return Integer.compare(this.order(),filter.order());
    }
}
